using System;
using UnityEngine;
using UnityEditor;
using RuleID = ProjScan.ScanRuleFixer.Rule.ProjectAssets;


namespace ProjScan.ScanRuleFixer
{
    class AnimationClipResampleCuve : IRuleFixer<RuleID.Animation.AnimationClip_ResampleCurve>
    {
        public bool Fix(string path)
        {
            try
            {
                string[] data = path.Split('$');
                string assetPath = data[0];

                ModelImporter importer = AssetImporter.GetAtPath(assetPath) as ModelImporter;
                importer.resampleCurves = false;
                importer.SaveAndReimport();

            }
            catch (Exception e)
            {
                Debug.Log(e.ToString());
                return false;
            }
            return true;
        }
    }

    class AnimationClipCompression : IRuleFixer<RuleID.Animation.AnimationClip_Compression>
    {
        public bool Fix(string path)
        {
            try
            {
                string[] data = path.Split('$');
                string assetPath = data[0];

                ModelImporter importer = AssetImporter.GetAtPath(assetPath) as ModelImporter;
                importer.animationCompression = ModelImporterAnimationCompression.Optimal;
                
                importer.SaveAndReimport();

            }
            catch (Exception e)
            {
                Debug.Log(e.ToString());
                return false;
            }
            return true;
        }
    }
    
    class AnimationClipFloatFormat : IRuleFixer<RuleID.Animation.AnimationClip_FloatFormat>
    {
        public bool Fix(string path)
        {
            try
            {
                // Assuming 'path' points to the animation clip asset we want to modify
                string[] data = path.Split('$');
                string assetPath = data[0];
                
                // 获取资产路径对应的AssetImporter
                AssetImporter assetImporter = AssetImporter.GetAtPath(assetPath);
                
                if (assetImporter != null && assetImporter is ModelImporter)
                {
                    Debug.LogWarning($"The AnimationClip at '{assetPath}' is imported from an external model file. Direct modification is not supported.");
                    return false; 
                }

                // Load the AnimationClip asset from the given path
                AnimationClip theAnimation = AssetDatabase.LoadAssetAtPath<AnimationClip>(assetPath);
                
                if (theAnimation == null)
                {
                    Debug.LogError($"Unable to load AnimationClip at path: {assetPath}");
                    return false;
                }

                // Retrieve all curves from the AnimationClip
                EditAnimationClipCurves(theAnimation);

                // Reimport the asset to apply the changes
                AssetDatabase.ImportAsset(assetPath);
                AssetDatabase.Refresh();
            }
            catch (Exception e)
            {
                Debug.Log(e.ToString());
                return false;
            }
            return true;
        }

        private static void EditAnimationClipCurves(AnimationClip theAnimation)
        {
            // Retrieve all curves from the animation clip
            AnimationClipCurveData[] curves = AnimationUtility.GetAllCurves(theAnimation, true);

            Keyframe key;
            Keyframe[] keyFrames;
            for (int ii = 0; ii < curves.Length; ++ii)
            {
                AnimationClipCurveData curveData = curves[ii];
                if (curveData.curve == null || curveData.curve.keys == null)
                {
                    continue;
                }
                keyFrames = curveData.curve.keys;
                for (int i = 0; i < keyFrames.Length; i++)
                {
                    key = keyFrames[i];
                    key.value = float.Parse(key.value.ToString("f3"));
                    key.inTangent = float.Parse(key.inTangent.ToString("f3"));
                    key.outTangent = float.Parse(key.outTangent.ToString("f3"));
                    keyFrames[i] = key;
                }
                curveData.curve.keys = keyFrames;
                theAnimation.SetCurve(curveData.path, curveData.type, curveData.propertyName, curveData.curve);
            }
        }
    }
}
